const util = require('./util.js')
const baseurl = util.baseurl

export { getPeopleList, getPeopleNum, addPeople, updatePeople, updatePeopleRegion, getPeopleDetail, getPeopleByRegion, searchPeople, uploadPeoplePhoto, getPeoplePhotoUrl };

// 获取人员列表请求
const getPeopleList = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        wx.request({
            url: baseurl + '/Family/ManagerGetFamilyFromRegion',
            method: 'POST',
            data: data,
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
// 获取人员总数量
const getPeopleNum = () => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Family/GetHouseholdCount',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
// 获取人员详情
const getPeopleDetail = (uid) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Family/GetOneFamilyWithUid',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: {
                'familyuid': uid
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
// 添加人员
const addPeople = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        console.log(data)
        wx.request({
            url: baseurl + '/Family/EnrollFamilyInformation',
            method: 'POST',
            data: data,
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//上传人员照片
const uploadPeoplePhoto = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.uploadFile({
            url: baseurl + '/Family/UploadHouseHoldSpousePhoto',
            filePath: data.File,
            name: 'file',
            formData: {
                'Uid': data.Uid
            },
            header: {
                'content-type': 'multipart/form-data',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//获取人员照片
const getPeoplePhotoUrl = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        wx.request({
            url: baseurl + '/Family/GetHouseHoldSpousePhoto',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: data,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
// 更新人员
const updatePeople = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Family/UpdateFamilyIfno',
            method: 'POST',
            data: data,
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
// 更新人员行政区划
const updatePeopleRegion = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Family/UpdateRegion',
            method: 'POST',
            data: data,
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

//行政区划获取人员
const getPeopleByRegion = (region) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(region)
        wx.request({
            url: baseurl + '/Family/GetHouseHoldListByAdministrativeDivision',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: {
                'administrativeDivision': region
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//搜索人员
const searchPeople = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        wx.request({
            url: baseurl + '/Family/SearchFamilies',
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },

            data: JSON.stringify(data),
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}